# -*- coding:utf-8 -*-

from datetime import datetime
from sqlalchemy import Column, Integer, String, DateTime, Text
from config.database import Base


class ServerHost(Base):
    """服务器主机表"""
    __tablename__ = 'server_host'

    id = Column(Integer, primary_key=True, autoincrement=True, comment='主键')
    host_name = Column(String(100), nullable=False, comment='主机名称')
    host_ip = Column(String(50), nullable=False, index=True, comment='主机IP地址')
    internal_ip = Column(String(50), comment='内网IP地址（优先使用）')
    internal_ssh_port = Column(Integer, comment='内网SSH端口（为空则使用ssh_port）')
    ssh_port = Column(Integer, nullable=False, default=22, comment='SSH端口')
    ssh_username = Column(String(50), nullable=False, comment='SSH用户名')
    ssh_password = Column(String(200), comment='SSH密码（加密存储）')
    agent_path = Column(String(255), default='/home/project/', comment='Agent项目路径')
    agent_status = Column(String(20), default='unknown', comment='Agent状态: unknown/stopped/running/error')
    agent_version = Column(String(50), comment='Agent版本号')
    deploy_status = Column(String(20), default='not_deployed', comment='部署状态: not_deployed/deploying/deployed/deploy_failed')
    deploy_error = Column(Text, comment='部署错误信息')
    connection_status = Column(String(20), default='disconnected', comment='连接状态: connected/disconnected/error')
    latency = Column(Integer, comment='连接延迟（毫秒，最后一次测试）')
    connection_type = Column(String(20), comment='连接类型: 内网/外网/外网（降级）')
    last_connect_time = Column(DateTime, comment='最后连接时间')
    last_deploy_time = Column(DateTime, comment='最后部署时间')
    remark = Column(String(500), comment='备注')
    status = Column(String(1), nullable=False, default='0', comment='状态（0正常 1停用）')
    create_time = Column(DateTime, nullable=False, default=datetime.now, comment='创建时间')
    update_time = Column(DateTime, nullable=False, default=datetime.now, onupdate=datetime.now, index=True, comment='更新时间')
    del_flag = Column(String(1), nullable=False, default='0', comment='删除标志（0代表存在 2代表删除）')
    create_by = Column(Integer, nullable=False, comment='创建者')
    dept_id = Column(Integer, nullable=False, comment='部门id')


class ServerCommandLog(Base):
    """服务器命令执行日志表"""
    __tablename__ = 'server_command_log'

    id = Column(Integer, primary_key=True, autoincrement=True, comment='主键')
    host_id = Column(Integer, index=True, comment='主机ID（NULL表示批量执行）')
    host_ip = Column(String(50), comment='主机IP')
    command = Column(Text, nullable=False, comment='执行的命令')
    command_type = Column(String(50), default='custom', comment='命令类型: custom/deploy/start/stop/restart')
    result = Column(Text, comment='执行结果')
    error = Column(Text, comment='错误信息')
    status = Column(String(20), nullable=False, default='pending', comment='执行状态: pending/success/failed')
    execute_time = Column(DateTime, comment='执行时间')
    duration = Column(Integer, comment='执行耗时（毫秒）')
    create_time = Column(DateTime, nullable=False, default=datetime.now, index=True, comment='创建时间')
    create_by = Column(Integer, nullable=False, comment='创建者')

